<?php


namespace App\Http\Controllers\Admin\PlatformManage;

use App\Http\Controllers\Controller;
use App\Http\Requests\PlatformCerateRequest;
use App\Http\Requests\PlatformPasswordRequest;
use App\Http\Requests\PlatformSecretRequest;
use App\Http\Requests\PlatformUpdateRequest;
use App\Models\Admin\AdminRoleModel as Role;
use App\Services\AdminService\Services\UserService;
use App\Response\AdminRep;
use App\Response\TableRep;
use App\Services\PlatformService\Models\PlatformModel;
use App\Services\PlatformService\Services\PlatformService;
use http\Client\Curl\User;
use Illuminate\Http\Request;

class PlatformController extends Controller
{
    use AdminRep, TableRep;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return view('admin.platform_manage.platform.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.platform_manage.platform.create');
    }

    /**
     * 获取数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $res = PlatformService::getData(new PlatformModel, $request->all());
        return $this->table($res);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(PlatformCerateRequest $request)
    {
        $data = $request->all();
        if (PlatformService::createPlatfrom($data)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * 更新secret
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function secret(PlatformSecretRequest $request)
    {
        $data = $request->all();
        $model = PlatformService::updateSecretById($data['id']);
        if ($model) {
            return $this->responseSucc($model->secret);
        }
        return $this->responseFail('Secret生成失败,请重新生成');
    }

    /**
     * 生成secret 验证密码
     * @param Request $request
     */
    public function checkPassword(PlatformPasswordRequest $request)
    {
        $data = $request->all();//用户现在输入的密码
        $result = UserService::checkPassword($data['password']);
        if ($result) {
            return $this->responseSucc();
        }
        return $this->responseFail('密码错误');
    }


    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = PlatformService::getByIdOrFail($id);
        return view('admin.platform_manage.platform.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PlatformUpdateRequest $request, $id)
    {
        $user = PlatformService::getByIdOrFail($id);
        $data = $request->except('password');
        if ($user->update($data)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return response()->json(['code' => 1, 'msg' => '请选择删除项']);
        }
        if (PlatformService::destroy($ids)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }
        return response()->json(['code' => 1, 'msg' => '删除失败']);
    }


}